//
//  youhuiquanVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/3/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "youhuiquanVC.h"

#import "Masonry.h"
#import "TongYong.h"


#import "Define.h"

#import "Controller.h"

#import "youhuiModel.h"

#import "labelview.h"

#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1]
#define Color153  [UIColor colorWithRed:80.f/255.f green:80.f/255.f blue:80.f/255.f alpha:1]
#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height


@interface youhuiquanVC ()<UITableViewDataSource,UITableViewDelegate>
{

    //刷新数据Array;
    NSMutableArray *ArraY;//可用
    
    youhuiModel *youhuiMd;
    
    NSString *isCanuse;
    
    UIButton *canuseBUt;//可用按钮
    
    UIButton *notuseBUt;//不可用按钮
    
    UIView *viewImge;//写划线标记
    
    UILabel *nameText;
    
    UIButton *selectCellBUt;//不可用按钮
    
    NSInteger selectNum;//选中的索引
    
    UIButton *sureBut;//确定订单
    
    labelview *la;//无数据
    
}
@property(nonatomic,strong) UITableView *tableview;  //定义大表格
@end

@implementation youhuiquanVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"youhuiquanVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"youhuiquanVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden=YES;
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(60);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"优惠券";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    
    //顶部按钮
    UIView *Topview=[[UIView alloc]initWithFrame:CGRectMake(0, 60, screenWidth, 50)];
    Topview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:Topview];
    
    canuseBUt=[[UIButton alloc]initWithFrame:CGRectMake(0,0, screenWidth/2, 50)];
    canuseBUt.backgroundColor=[UIColor clearColor];
    [canuseBUt setTitle:@"可用优惠券" forState:UIControlStateNormal];
    canuseBUt.titleLabel.font=[UIFont systemFontOfSize:14];
    [canuseBUt setTitleColor:[UIColor colorWithRed:248.f/255.f green:89.f/255.f blue:60.f/255.f alpha:1] forState:UIControlStateNormal];
    [canuseBUt addTarget:self action:@selector(canuseBUtAction) forControlEvents:UIControlEventTouchUpInside];
    [canuseBUt setSelected:YES];
    [Topview addSubview:canuseBUt];
    
    
    
    notuseBUt=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2,0, screenWidth/2, 50)];
    notuseBUt.backgroundColor=[UIColor clearColor];
    [notuseBUt setTitle:@"不可用优惠券" forState:UIControlStateNormal];
    notuseBUt.titleLabel.font=[UIFont systemFontOfSize:14];
    [notuseBUt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [notuseBUt addTarget:self action:@selector(notuseBUtAction) forControlEvents:UIControlEventTouchUpInside];
    [notuseBUt setSelected:NO];
    [Topview addSubview:notuseBUt];

    viewImge=[[UIView alloc]init ];
    viewImge.frame=CGRectMake((screenWidth/2-100)/2, 49, 100, 1);
    viewImge.backgroundColor=[UIColor colorWithRed:248.f/255.f green:89.f/255.f blue:60.f/255.f alpha:1];
    [Topview addSubview:viewImge];
    
    
    isCanuse=@"可用";
    
    selectNum=-1;
    
    ArraY=[[NSMutableArray alloc]init];
    

    //获取优惠券接口数据
    [self getyouhuiquan];
    
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,120, self.view.frame.size.width, self.view.frame.size.height-120-50)];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    [self.view addSubview:_tableview];
    
    //下部按钮
    UIView *bootmView=[[UIView alloc]init];
    bootmView.backgroundColor=[UIColor clearColor];
    bootmView.frame=CGRectMake(0,screenHeight-50, screenWidth, 50);
    [self.view addSubview:bootmView];

    sureBut=[[UIButton alloc]initWithFrame:CGRectMake((screenWidth-248)/2,5, 248, 40)];
    sureBut.backgroundColor=[UIColor colorWithRed:248.f/255.f green:89.f/255.f blue:60.f/255.f alpha:1];
    [sureBut setTitle:@"确定" forState:UIControlStateNormal];
    sureBut.titleLabel.font=[UIFont systemFontOfSize:16];
    [sureBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBut addTarget:self action:@selector(sureButAction) forControlEvents:UIControlEventTouchUpInside];
    sureBut.layer.masksToBounds=YES;
    sureBut.hidden=YES;
    sureBut.layer.cornerRadius=20;
    [bootmView addSubview:sureBut];
    

}
//可用按钮
-(void)canuseBUtAction
{
    if (canuseBUt.isSelected==NO) {
        [notuseBUt setSelected:NO];
        [canuseBUt setSelected:YES];
        viewImge.frame=CGRectMake((screenWidth/2-100)/2, 49, 100, 1);
        
        [ArraY removeAllObjects];
        
        NSMutableArray *arraymu=[[NSMutableArray alloc]initWithArray:youhuiMd.CanUsedArray];
        
        ArraY=arraymu;
        
        //判断 是否  有 无数据
        if (ArraY.count==0) {
            if (la) {
                [la removeFromSuperview];
            }
            
            la=[[labelview alloc]init];
            [la setimage:@"youhuiquanNew" tit:@"暂无可用优惠券" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
            la.image.frame=CGRectMake((self.view.frame.size.width-110)/2, 50, 110, 119);
            la.label.frame=CGRectMake(50, 180, self.view.frame.size.width-100, 30);
            [_tableview addSubview:la];
            
            sureBut.hidden=YES;
        }else{
            
            if (la) {
                [la removeFromSuperview];
            }
            
            sureBut.hidden=NO;
            
            [self.tableview reloadData];
            
        }
    }
    

}
//不可用按钮
-(void)notuseBUtAction
{

    if (notuseBUt.isSelected==NO) {
        sureBut.hidden=YES;
        [canuseBUt setSelected:NO];
        [notuseBUt setSelected:YES];
        viewImge.frame=CGRectMake(screenWidth/2+(screenWidth/2-100)/2, 49, 100, 1);
        
        [ArraY removeAllObjects];
        
        NSMutableArray *arraymu=[[NSMutableArray alloc]initWithArray:youhuiMd.NotUsedArray];
        
        ArraY=arraymu;
        
        //判断 是否  有 无数据
        if (ArraY.count==0) {
            if (la) {
                [la removeFromSuperview];
            }
            
            la=[[labelview alloc]init];
            [la setimage:@"youhuiquanNew" tit:@"您暂无不可用优惠券" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
            la.image.frame=CGRectMake((self.view.frame.size.width-110)/2, 50, 110, 119);
            la.label.frame=CGRectMake(50, 180, self.view.frame.size.width-100, 30);
            [_tableview addSubview:la];
            
        }else{
            
            if (la) {
                [la removeFromSuperview];
            }
            
            [self.tableview reloadData];
            
        }

    
    }

}

-(void)getyouhuiquan
{
 
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    [di setValue:@"1433739374266387" forKey:@"MEMBER_ID"];

    if ([self.isgouwuche isEqualToString:@"购物车"]) {
      [di setObject:self.productArryStr forKey:@"PRODUCT_IDS"];
        
    }else{
    
        [di setObject:self.productArryStr forKey:@"PRODUCT_ID"];
        
        [di setObject:self.QUANTITY forKey:@"QUANTITY"];
    }
    [Controller requestWithURL:COUNPONLIST jsondic:di finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            youhuiMd=[[youhuiModel  alloc]initWith:data.data];
            
            NSMutableArray *arraymu=[[NSMutableArray alloc]initWithArray:youhuiMd.CanUsedArray];
            
            ArraY=arraymu;
            
            
            
//            CanUsed =     (
//                           {
//                               "BATCH_DETAIL_CODE" = PCM7k5h857yux;
//                               CONDITION = "\U8d2d\U8d27";
//                               "COUPON_RULES" = "\U6ee1&yen;5000\U51cf5\U5143,\U6ee1&yen;8000\U51cf8\U5143,\U6ee1&yen;10000\U51cf10\U5143";
//                               "DISCOUNT_VALUE" = 10;
//                               "VALID_TIME" = "2016.03.10-2016.03.31";
//                           }
//                           );
//            CouponCount = 1;
//            NotUsed =     (
//            );
            
            
            
            //判断 是否  有 无数据
            if (ArraY.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }
                
                la=[[labelview alloc]init];
                [la setimage:@"youhuiquanNew" tit:@"暂无可用优惠券" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.image.frame=CGRectMake((self.view.frame.size.width-110)/2, 50, 110, 119);
                la.label.frame=CGRectMake(50, 180, self.view.frame.size.width-100, 30);
                [_tableview addSubview:la];
                sureBut.hidden=YES;
                
            }else{
                
                if (la) {
                    [la removeFromSuperview];
                }
                
                sureBut.hidden=NO;
                [self.tableview reloadData];
                
            }

            
            
        
            
        }else
        {
            ALERTSHOW(data.message);
            
            
        }
    }];
    
    
    
    
}







- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (ArraY.count>0) {
        return ArraY.count;
    }
    
    return 0;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 40;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

        static NSString *CellIdentifier1 = @"Cell";
    
        UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.backgroundColor=[UIColor whiteColor];
        
            nameText=[[UILabel alloc]initWithFrame:CGRectMake(50, 10, screenWidth-60, 20)];
            nameText.font=[UIFont systemFontOfSize:12];
            nameText.textAlignment=NSTextAlignmentLeft;
            nameText.tag=indexPath.row+1000;
            nameText.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
            [cell addSubview:nameText];
            
            if ([isCanuse isEqualToString:@"可用"]) {
            
                selectCellBUt=[UIButton buttonWithType:UIButtonTypeCustom];
                selectCellBUt.selected=NO;
//                [selectCellBUt setBackgroundImage:[UIImage imageNamed:@"ic_radio_btn_normal"] forState:UIControlStateNormal];
                selectCellBUt.frame=CGRectMake(10, 7.5, 25, 25);
                selectCellBUt.layer.masksToBounds=YES;
                selectCellBUt.layer.cornerRadius=10;
                selectCellBUt.tag=200+indexPath.row;
                [selectCellBUt addTarget:self action:@selector(selectCellBUtClick:) forControlEvents:UIControlEventTouchUpInside];
        
                
                [cell addSubview:selectCellBUt];
            }
        
    
        }
    
    NSString *str=[NSString stringWithFormat:@"%@",ArraY[indexPath.row][@"COUPON_RULES"]];
    
    NSString *strUrl = [str stringByReplacingOccurrencesOfString:@"&yen;" withString:@"￥"];
    
    UILabel *namela=[cell viewWithTag:indexPath.row+1000];
    namela.text=strUrl;
    
    selectCellBUt=[cell viewWithTag:200+indexPath.row];
    
    
    if ([isCanuse isEqualToString:@"可用"]) {
    
      if (indexPath.row==selectNum) {
        
        [selectCellBUt setBackgroundImage:[UIImage imageNamed:@"ic_apply_quality_selected"] forState:UIControlStateNormal];
        
      }else{
      
        [selectCellBUt setBackgroundImage:[UIImage imageNamed:@"ic_radio_btn_normal"] forState:UIControlStateNormal];
      }
    }
    
    
        return cell;
}

//选中单元格时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    // 取消选中状态
    //    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

-(void)selectCellBUtClick:(UIButton *)selsectBut
{
   
    UITableViewCell * cell = (UITableViewCell *)[[selsectBut superview] superview];
    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
    selectNum=selsectBut.tag-200;
    
//    selectNum=indexPath.row;
    
    [self.tableview reloadData];

}

//确定按钮
-(void)sureButAction
{

    if (selectNum>=0) {
        NSDictionary *dicyouhui=ArraY[selectNum];
        //当前是列表页的时候可以直接相应通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"upyouhui" object:dicyouhui];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
    
    
    }
    
    
}


-(void)backLogin
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
